// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Breakable Wall Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 43 : TYPE_BREAKABLEWALL

// Function declarations
reserve function BreakableWall_DebugDraw
reserve function BreakableWall_DebugSpawn

// Static Values

// Tables

function BreakableWall_DebugDraw
	DrawSprite(0)
end function


function BreakableWall_DebugSpawn
	CreateTempObject(TypeName[Breakable Wall], 0, object.xpos, object.ypos)
	object[tempObjectPos].propertyValue = 0
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
	if object.propertyValue == 1
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x7000
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			checkResult = 0
			if object[currentPlayer].state == PlayerObject_HandleRoll
				if object[currentPlayer].speed > 0x47FFF
					checkResult = 1
				end if
				if object[currentPlayer].speed < -0x47FFF
					checkResult = 1
				end if
			end if
			if stage.playerListPos == 2
				checkResult = 1
			end if
			if stage.playerListPos == 6
				checkResult = 1
			end if			
			if PlayerObject_SuperState == 1
				checkResult = 1
			end if
			if object[currentPlayer].state == PlayerObject_HandleAir
				if object[currentPlayer].value37 == 3
					if object[currentPlayer].animation == ANI_JUMPING
						if object[currentPlayer].speed > 0x47FFF
							checkResult = 1
						end if
						if object[currentPlayer].speed < -0x47FFF
							checkResult = 1
						end if
					end if
				end if
			end if
			if object[currentPlayer].animation == ANI_HAMMER_SPEEN
				checkResult = 1
			end if
			CallFunction(PlayerObject_WallBreaker)
			if temp0 == 1
				checkResult = 1
			end if
			if checkResult == 1
				BoxCollisionTest(C_TOUCH, object.entityPos, -17, -32, 17, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				CallFunction(PlayerObject_WallBreaker)
				if temp0 == 1
					checkResult = 1
				end if		
				if checkResult == 1
					object.type = TypeName[Blank Object]
					PlaySfx(SfxName[Block Break], 0)
					temp2 = object.xpos
					temp2 -= 0x80000
					temp3 = object.xpos
					temp3 += 0x80000
					temp4 = object.ypos
					temp4 -= 0x180000
					if object[currentPlayer].xpos < object.xpos
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4
					else
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], 1, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], 1, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4
					end if
					if object[currentPlayer].animation == ANI_GLIDING
						object[currentPlayer].animation = ANI_GLIDING_DROP
						object[currentPlayer].state = PlayerObject_KnuxGlideDrop
						if object[currentPlayer].direction == FLIP_X
							FlipSign(object[currentPlayer].speed)
						end if
					end if
				end if
			else
				BoxCollisionTest(C_BOX, object.entityPos, -16, -32, 16, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
		next
	end if
end event


event ObjectDraw
	DrawSprite(object.propertyValue)
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Breakable Wall]
			object[arrayPos0].propertyValue = 0
			object[arrayPos0].drawOrder = 4
		end if
		arrayPos0++
	loop
	SpriteFrame(-16, -32, 32, 64, 34, 26)
	SpriteFrame(-8, -8, 16, 16, 34, 26)
	SetTableValue(TypeName[Breakable Wall], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BreakableWall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BreakableWall_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
